<?php

/**
 * 订单项目控制器
 *
 * @since 1.0 2014-12-20 by sutroon
 */
class OrderItemsAction extends BaseAction {

    public function index() {
        
    }

    /**
     * 获取购物车列表
     * 返回格式：id, productName, catalogID, price, number, discountRate, discountAmount, subtotal
     * @param int $sellerUserID
     * @return false|array
     * @since 1.0 2014-12-20 by sutroon
     */
    public static function get_shoppingcar_list($sellerUserID, $orderID = 0) {
        $where = $orderID > 0 ? "c.orderID=$orderID" : "c.orderID = 0 and c.sellerUserID=$sellerUserID";
        return M()->query('select c.id as carid, p.id, p.productName, p.catalogID, c.price, c.number, c.discountRate, c.discountAmount, c.subtotal from torder_items c left join tproduct_goods p on c.productID=p.id where ' . $where);
    }

    /**
     * 检测购物车是否有商品
     * @param int $sellerUserID
     * @return boolean
     * @since 1.0 2014-12-20 by sutroon
     */
    public static function exists_car($sellerUserID) {
        return M('torder_items')->where("orderID=0 and sellerUserID=$sellerUserID")->count('id') > 0;
    }

    /**
     * 获取购物车统计数据
     * 数据格式array('count' => $count, 'sum' => $sum);
     * @param int $sellerUserID
     * @return false|array
     * @since 1.0 2014-12-20 by sutroon
     */
    public static function get_car_summary($sellerUserID, $orderID = 0) {
        if (!$sellerUserID && !$orderID) {
            return false;
        }
        $list = self::get_shoppingcar_list($sellerUserID, $orderID);
        if (false === $list) {
            return false;
        }
        $sum = 0;
        $count = count($list);
        foreach ($list as $p) {
            $sum+=$p['subtotal'];
        }
        return array('count' => $count, 'sum' => $sum);
    }

    /**
     * 清空购物车
     * @param int $sellerUserID
     * @since 1.0 2014-12-20 by sutroon
     */
    public static function clear_car($sellerUserID, $orderID = 0) {
        $where = $orderID > 0 ? "orderID=$orderID" : "orderID=0 and sellerUserID=$sellerUserID";
        $result = M('torder_items')->where($where)->delete();
        return $result;
    }

    /**
     * 保存购物车到订单项目中
     * @param int $buyerUserID
     * @param int $sellerUserID
     * @param string $orderSerialNo
     * @param int $orderID
     * @return array array('success'=>boolean, 'message'=>string|array('count','amount'));
     * @since 1.0 2014-12-20 by sutroon
     */
    public static function save_to_orderitems($buyerUserID, $sellerUserID, $orderSerialNo, $orderID = 0) {
        $products = self::get_shoppingcar_list($sellerUserID, $orderID);
        if (!$products) {
            return array('success' => false, 'message' => '购物车没有商品!');
        }
        if (!$orderID) {
            $orderID = 0;
        }
        $itemCount = count($products);
        $amount = 0;
        foreach ($products as $p) {
            $amount+=$p['subtotal'];
            $sqls[] = "(" . $p['carid'] . ", $orderID, $sellerUserID, '$orderSerialNo', " . $p['id'] . ", " . $p['number'] . ", " . $p['price'] . ", " . $p['discountRate'] . ", " . $p['discountAmount'] . ", " . $p['subtotal'] . ", " . $p['catalogID'] . ")";
        }
        $result = M()->execute("replace into torder_items (id, orderID, sellerUserID, orderSerialNo, productID, number, price, discountRate, discountAmount, subtotal, catalogID) values " . implode(',', $sqls));
        if (false === $result) {
            return array('success' => false, 'message' => '购物车保存失败!');
        }
        return array('success' => true, 'message' => array('count' => $itemCount, 'amount' => $amount));
    }

    // 返回购物车列表 2014-12-18 by sutroon
    public function list_json() {
        $sellerUserID = I('sellerUserID');
        $orderID = I('orderID');
        if (!$sellerUserID && !$orderID) {
            $this->act_ajaxReturn(false, '参数有误!');
        }
        $this->ajaxReturn(self::get_shoppingcar_list($sellerUserID, $orderID));
    }

    // 保存到购物车 2014-12-18 by sutroon
    public function item_save() {
        $productID = I('productID');
        $number = I('number');
        $discountRate = I('discountRate');
        $sellerUserID = I('sellerUserID');
        $orderID = I('orderID');
        $method = I('method');
        if (!$productID || !$number || !$method || (!$sellerUserID && !$orderID)) {
            $this->act_ajaxReturn(false, '参数有误!');
        }
        if (!is_numeric($discountRate)) {
            $discountRate = 100;
        }
        $product = M('tproduct_goods')->find($productID);
        if (!$product) {
            $this->act_ajaxReturn(false, '商品不存在');
        }
        $M = M('torder_items');
        $where = $orderID && $orderID > 0 ? "orderID=$orderID and productID=$productID" : "orderID=0 and sellerUserID=$sellerUserID and productID=$productID";
        $data = $M->where($where)->find();
        if ($data) {
            if ($method == 'ADD') {
                $data['number'] = $data['number'] + $number;
            } else {
                $data['number'] = $number;
            }
        } else {
            $data['number'] = $number;
            $data['productID'] = $productID;
            $data['sellerUserID'] = $sellerUserID;
            if ($orderID) {
                $data['orderID'] = $orderID;
            }
        }
        $data['price'] = $product['price'];
        $data['discountRate'] = $discountRate;
        $subtotal = $data['number'] * $product['price'];
        $discountAmount = 0;
        if ($discountRate !== '100') {
            $amount = $subtotal * $discountRate / 100;
            $discountAmount = $subtotal - $amount;
            $subtotal = $amount;
        }
        $data['subtotal'] = $subtotal;
        if ($data['id']) {
            $result = $M->save($data);
        } else {
            $result = $M->add($data);
        }
        $this->act_ajaxReturn($result, '加入购物车%!');
    }

    // 从购物车删除 2014-12-18 by sutroon
    public function item_delete() {
        $sellerUserID = I('sellerUserID');
        $productID = I('productID');
        $orderID = I('orderID');
        if (!$productID || (!$sellerUserID && !$orderID)) {
            $this->act_ajaxReturn(false, '参数有误');
        }
        $where = ($orderID && $orderID > 0) ? "orderID=$orderID and productID=$productID" : "orderID=0 and sellerUserID=$sellerUserID and productID=$productID";
        $result = M('torder_items')->where($where)->delete();
        $this->act_ajaxReturn($result, '删除%!');
    }

    // 清空购物车 2014-12-20 by sutroon
    public function clear() {
        $sellerUserID = I('sellerUserID');
        $orderID = I('orderID');
        if (!$sellerUserID && !$orderID) {
            $this->act_ajaxReturn(false, '参数有误!');
        }
        $where = ($orderID && $orderID > 0) ? "orderID=$orderID" : "orderID=0 and sellerUserID=$sellerUserID";
        $result = M('torder_items')->where($where)->delete();
        $this->act_ajaxReturn($result, '清空%!');
    }

    // 获取统计数据 2014-12-18 by sutroon
    public function subtotal_json() {
        $sellerUserID = I('sellerUserID');
        $orderID = I('orderID');
        if (!$sellerUserID && !$orderID) {
            $this->ajaxReturn(false);
        }
        $this->ajaxReturn(self::get_car_summary($sellerUserID, $orderID));
    }

}
